#!/usr/bin/env python
# -*- coding: UTF-8 -*-
'''
Created on 2011/03/23

@author: Rafael
'''

# processamento de HTML atrav�s do parser BeautifulSoup
from BeautifulSoup import BeautifulSoup
import re
import os.path
from unicodedata import normalize

# biblioteca para acesso atrav�s de protocolos de Internet
import urllib2

# escrita em csv
import csv
import sqlite3

from SqlInstructions import SqlInstructions
from WriteInfo import WriteInfo
from Servidor import Servidor
import os
from CreateGraf import CreateGraf
from threading import *

'''
projecto- classe destinada a leitura da informa��o no sistema rebides
autor- Jose Jasnau Caeiro
data de cria��o do m�dulo- 7/03/2011
observa��es-
'''

class Rebides():

    #construtor
    def __init__(self):
        self.sql = SqlInstructions()
        pass
    
    #Create the graphics
    def CreateGraf(self, anos, tipoGraf):
        
        self.checkDB(anos)
        CreateGraf(anos, tipoGraf)

        pass
    
    #create the HTML
    def CreateHtml(self, anos):
        self.checkDB(anos)
        
        for ano in anos:
            try:
                os.stat('html/200{0}'.format(ano))
                pass
            except:
                self.sql.doQueriesHTML(ano)
                pass
            pass
        WriteInfo().doIndex()
        pass

    #create the CSV
    def CreateCsv(self, anos, campos):
        self.checkDB(anos)

        for ano in anos:
            self.sql.doQueriesCSV(ano, campos)
            pass
        
        pass

    #check if theDB is in the computer
    def checkDB(self, anos):
        for ano in anos:
            if os.path.exists('db/rebides0{0}.db'.format(ano)):
                pass
            else:
                if os.path.exists('db'):
                    pass
                else:
                    os.mkdir('db')
                self.sql.preencher_tabelas_gerais(ano)
                pass
            pass
        pass
    
    #Start the webserver
    def startServer(self):
        self.server = Servidor()
        self.server.startServer()
        pass
    
    #stops the webserver
    def stopServer(self):
        self.server.stopServer()
        pass
pass
